
package com.hlkj.pay.app.merchant.ext.impl.yytpay;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.alibaba.fastjson.JSONArray;
import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.framework.extension.annotation.Extension;
import com.hlkj.pay.app.merchant.ext.ChannelMerchantAccessExtPt;
import com.hlkj.pay.app.merchant.ext.MerchantExtConstant;
import com.hlkj.pay.app.merchant.ext.dto.access.*;
import com.hlkj.pay.app.merchant.ext.impl.haike.dto.HaikePayAppChannelParams;
import com.hlkj.pay.app.merchant.ext.impl.yytpay.constant.YytPayConstant;
import com.hlkj.pay.app.merchant.ext.impl.yytpay.dto.YytPayConfig;
import com.hlkj.pay.app.merchant.ext.impl.yytpay.dto.resp.*;
import com.hlkj.pay.app.merchant.ext.impl.yytpay.handler.YytPayCommonParamsHandle;
import com.hlkj.pay.dto.merchant.AccessChannelParamsDto;
import com.hlkj.pay.dto.merchant.MerchantAccessDto;
import com.hlkj.pay.dto.merchant.MerchantAccessSignLink;
import com.hlkj.pay.enums.MerchantEnum;
import com.hlkj.pay.enums.PayEnums;
import com.hlkj.pay.infrastructure.model.merchant.MerchantAccessCertInfoDO;
import com.hlkj.pay.infrastructure.model.merchant.MerchantAccessInfoDO;
import com.hlkj.pay.infrastructure.model.pay.PayChannelRequestDO;
import com.hlkj.pay.infrastructure.model.pay.ProviderChannelConfigDO;
import com.hlkj.pay.vo.order.resp.MerchantProductQueryResp;
import com.yst.m2.sdk.ReturnObj;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import jodd.util.StringPool;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 银盈通进件操作
 */
@Slf4j
@Repository
@RequiredArgsConstructor
@Extension(bizId = MerchantExtConstant.CHANNEL_YYT_PAY)
public class YytPayMerchantAccessExtPt implements ChannelMerchantAccessExtPt {

    private final YytPayCommonParamsHandle yytPayCommonParamsHandle;

    @Value("${hlkj.domain}")
    private String domain;

    @Override
    public void channelConfigCheck(ChannelMerchantAccessCommonContext channelMerchantAccessCommonContext) {

    }

    @Override
    public void merchantAccess(ChannelMerchantAccessContext channelMerchantAccessContext) {
        log.info("银盈通渠道提交进件 请求参数:{}", JsonUtils.toJsonString(channelMerchantAccessContext));
        CommonResult<YytPayConfig> yytPayConfigCommonResult = yytPayCommonParamsHandle.yytPayConfig(channelMerchantAccessContext.getProviderChannelConfig(),
                channelMerchantAccessContext.isTest());
        if (!yytPayConfigCommonResult.isSuccess()) {
            channelMerchantAccessContext.setRemark(yytPayConfigCommonResult.getMsg());
            channelMerchantAccessContext.setProcessStatus(false);
            return;
        }
        MerchantAccessDto merchantAccessDto = channelMerchantAccessContext.getMerchantAccessDto();
        MerchantAccessInfoDO merchantAccessInfo = merchantAccessDto.getMerchantAccessInfo();
        // 初始化银盈通配置
        YytPayConfig yytPayConfig = yytPayConfigCommonResult.getData();
        // 企业类型
        MerchantEnum.MERCHANT_TYPE merchantType = MerchantEnum.MERCHANT_TYPE.from(merchantAccessInfo.getType());

        PayChannelRequestDO payChannelRequestDO = new PayChannelRequestDO();
        channelMerchantAccessContext.setPayChannelRequestDO(payChannelRequestDO);

        try {

            // 请求参数
            Map<String,Object> data = new HashMap<>();
            data.put("user_id",yytPayConfig.getUserId());
            data.put("is_isp_open","1");
            data.put("isp_ent_no",yytPayConfig.getIspEntNo());
            data.put("login_token", "");//登录令牌
            data.put("req_no", IdUtil.simpleUUID());//请求流水号
            data.put("plat_form", "18");

            // 商户基本信息
            data.put("short_name",merchantAccessDto.getMerchantAccessBusiness().getShortName());

            if(merchantType == MerchantEnum.MERCHANT_TYPE.COMPANY){
                data.put("ent_name",merchantAccessDto.getMerchantAccessLicense().getName());
                data.put("ent_type",yytPayCommonParamsHandle.companyType(Integer.valueOf(merchantAccessDto.getMerchantAccessLicense().getCompanyType()),merchantType));
            }else{
                data.put("ent_name",merchantAccessDto.getMerchantAccessLegal().getName());
                data.put("ent_type","3");
            }
            // 企业属性
            data.put("ent_attr","3");
            // 商户分类
            data.put("mer_type","4");
            // 营业执照信息  企业必传
            if(merchantType == MerchantEnum.MERCHANT_TYPE.COMPANY){
                data.put("ent_code",merchantAccessDto.getMerchantAccessLicense().getLicenseCode());
                data.put("license_from",merchantAccessDto.getMerchantAccessLicense().getLicenseBeginDate());
                if(merchantAccessDto.getMerchantAccessLicense().getLicenseValidityType() == 1){
                    data.put("license_end","9999-12-31");
                }else{
                    data.put("license_end",merchantAccessDto.getMerchantAccessLicense().getLicenseEndDate());
                }
                data.put("register_address",merchantAccessDto.getMerchantAccessLicense().getAddress());
                // 执照图片id
                data.put("license_photo",yytPayCommonParamsHandle.getUploadMerQualUrl("企业执照照片",merchantAccessDto.getMerchantAccessLicense().getLicenseUrl(),yytPayConfig));
            }else{
                data.put("ent_code",merchantAccessDto.getMerchantAccessLegal().getCertNo());
                data.put("license_photo",yytPayCommonParamsHandle.getUploadMerQualUrl("小微商户身份照片",merchantAccessDto.getMerchantAccessLegal().getCertFrontUrl(),yytPayConfig));
                data.put("register_address",merchantAccessDto.getMerchantAccessLegal().getAddress());
                data.put("license_from",merchantAccessDto.getMerchantAccessLegal().getCertBeginDate());
                if(merchantAccessDto.getMerchantAccessLegal().getCertValidityType() == 1){
                    data.put("license_end","9999-12-31");
                }else{
                    data.put("license_end",merchantAccessDto.getMerchantAccessLegal().getCertEndDate());
                }
            }

            // 实际经营地址
            data.put("province_code",merchantAccessDto.getMerchantAccessBusiness().getProvinceCode());
            data.put("city_code",merchantAccessDto.getMerchantAccessBusiness().getCityCode());
            data.put("district_code",merchantAccessDto.getMerchantAccessBusiness().getAreaCode());
            data.put("bus_address",merchantAccessDto.getMerchantAccessBusiness().getAddress());

            // 商户门头
            data.put("store_entrance_pic",yytPayCommonParamsHandle.getUploadMerQualUrl("store_entrance_pic",merchantAccessDto.getMerchantAccessBusiness().getStoreHeaderUrl(),yytPayConfig));

            // 法人信息
            data.put("id_name",merchantAccessDto.getMerchantAccessLegal().getName());
            data.put("id_type","01");
            data.put("id_no",merchantAccessDto.getMerchantAccessLegal().getCertNo());
            data.put("id_positive",yytPayCommonParamsHandle.getUploadMerQualUrl("id_positive",merchantAccessDto.getMerchantAccessLegal().getCertFrontUrl(),yytPayConfig));
            data.put("id_negative",yytPayCommonParamsHandle.getUploadMerQualUrl("id_negative",merchantAccessDto.getMerchantAccessLegal().getCertBackUrl(),yytPayConfig));
            data.put("id_from",merchantAccessDto.getMerchantAccessLegal().getCertBeginDate());
            if(merchantAccessDto.getMerchantAccessLegal().getCertValidityType() == 1){
                data.put("id_end","9999-12-31");
            }else{
                data.put("id_end",merchantAccessDto.getMerchantAccessLegal().getCertEndDate());
            }

            // 商户联系人信息 联系人非同法
            if(merchantAccessDto.getMerchantAccessInfo().getContactType() != null && merchantAccessDto.getMerchantAccessInfo().getContactType() == 1){
                data.put("link_name",merchantAccessDto.getMerchantAccessLegal().getName());
                data.put("link_tel",merchantAccessDto.getMerchantAccessLegal().getPhone());

            }else{
                data.put("link_name",merchantAccessDto.getMerchantAccessContact().getName());
                data.put("link_tel",merchantAccessDto.getMerchantAccessContact().getPhone());
            }
            data.put("link_mail",merchantAccessDto.getMerchantAccessLegal().getOtherValue());
            data.put("login_account",merchantAccessDto.getMerchantAccessLegal().getOtherValue());

            // 行业类型
            data.put("trade_type",merchantAccessDto.getMerchantAccessBusiness().getMcc());

            // 结算信息
            Map<String,Object> settle_info = new HashMap<>();
            settle_info.put("bank_account",merchantAccessDto.getMerchantAccessCard().getCardNo());
            settle_info.put("bank_id",merchantAccessDto.getMerchantAccessCard().getBankCode());
            settle_info.put("bank_name",yytPayCommonParamsHandle.getBankName(merchantAccessInfo.getChannelCode(), PayEnums.DICT_KEY.BANK_CODE.getCode(),merchantAccessDto.getMerchantAccessCard().getBankCode()));
            // 对公/对私
            switch (MerchantEnum.CARD_TYPE.from(merchantAccessDto.getMerchantAccessCard().getType())){
                // 法人结算
                case CORPORATE_ACCOUNT:
                    settle_info.put("account_type","1");
                    break;
                // 对私法人结算
                case PRIVATE_CORPORATE_ACCOUNT:
                // 对私非法人结算
                case PRIVATE_NON_CORPORATE_ACCOUNT:
                    settle_info.put("account_type","3");
                    settle_info.put("account_phone",merchantAccessDto.getMerchantAccessCard().getCardPhone());
                    break;
            }
            data.put("settle_info",settle_info);

            // 受益人和实际控制人信息
            if(merchantType == MerchantEnum.MERCHANT_TYPE.COMPANY){
                MerchantAccessCertInfoDO merchantAccessController = null;
                if(merchantAccessInfo.getControllerType() != null && merchantAccessInfo.getControllerType() == 1){
                     merchantAccessController = merchantAccessDto.getMerchantAccessLegal();
                }else{
                    merchantAccessController = merchantAccessDto.getMerchantAccessController();
                }
                JSONArray  uboArray = yytPayCommonParamsHandle.getUnoArray(merchantAccessDto.getMerchantAccessBenefitList(), merchantAccessController,yytPayConfig);
                data.put("ent_dividend_person_list",JSONUtil.toJsonStr(uboArray));
            }

            // 记录请求参数
            payChannelRequestDO.setRequestId(merchantAccessInfo.getRequestId());
            payChannelRequestDO.setRequestParams(JsonUtils.toJsonString(data));
            payChannelRequestDO.setRequestUrl("gas_partn_api@enterprise_auth");
            payChannelRequestDO.setRequestTime(System.currentTimeMillis());

            // 是否修改
            boolean update = false;
            ReturnObj response = null;

            // 判断是否修改  注：统一社会信用代码（ent_code）字段不允许修改
            if(StrUtil.isNotEmpty(merchantAccessInfo.getSubMchId())){
                update = true;
                data.put("ent_no",merchantAccessInfo.getSubMchId());
                log.info("银盈通"+merchantType.getDesc()+"开户 修改请求参数 request:{}", JsonUtils.toJsonString(data));
                response = yytPayCommonParamsHandle.requestPostApi(YytPayConstant.URL_CA,"gas_partn_api@enterprise_update",JSONUtil.toJsonStr(data),yytPayConfig);
                log.info("银盈通"+merchantType.getDesc()+"开户 修改返回参数 response:{}", JsonUtils.toJsonString(response));
            }else{
                log.info("银盈通"+merchantType.getDesc()+"开户 请求参数 request:{}", JsonUtils.toJsonString(data));
                response = yytPayCommonParamsHandle.requestPostApi(YytPayConstant.URL_CA,"gas_partn_api@enterprise_auth",JSONUtil.toJsonStr(data),yytPayConfig);
                log.info("银盈通"+merchantType.getDesc()+"开户 返回参数 response:{}", JsonUtils.toJsonString(response));
            }

            payChannelRequestDO.setResponse(JSONUtil.toJsonStr(response));

            if(response.is_ok()){
                // 参数转化
                YytAccessResp accessResp = JSONUtil.toBean(response.get_data(),YytAccessResp.class);
                if("000000".equals(accessResp.getOp_ret_subcode())){
                    merchantAccessInfo.setSubMchId(accessResp.getEnt_no());
                    // 修改时 保存记录ID
                    if(update){
                        merchantAccessInfo.setExtendA(accessResp.getId());
                    }
                    // 下一步就是查询申请状态
                    channelMerchantAccessContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.CHANNEL_APPROVE);
                    channelMerchantAccessContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.SUBMIT);
                }else{
                    // 下一步就是查询申请状态
                    channelMerchantAccessContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
                    channelMerchantAccessContext.setRemark(accessResp.getOp_err_submsg());
                    channelMerchantAccessContext.setProcessStatus(Boolean.FALSE);
                }

            }else{
                channelMerchantAccessContext.setRemark(response.get_err_msg());
                channelMerchantAccessContext.setProcessStatus(Boolean.FALSE);
                channelMerchantAccessContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
            }

        } catch (Exception e) {
            log.error("银盈通开户请求失败,失败信息 message: {}", e);
            channelMerchantAccessContext.setRemark("银盈通开户请求失败,失败信息, " + e.getMessage());
            channelMerchantAccessContext.setProcessStatus(Boolean.FALSE);
            channelMerchantAccessContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
        }

    }

    @Override
    public String requestUrl(MerchantAccessDto merchantAccessInfoDO) {
        return null;
    }

    @Override
    public String channelParams(AccessChannelParamsDto accessChannelParamsDto) {
        HaikePayAppChannelParams params = new HaikePayAppChannelParams();
        params.setSubMchId(accessChannelParamsDto.getSubMchId());
        params.setPn(accessChannelParamsDto.getExtendB());
        return JsonUtils.toJsonStringNotNull(params);
    }

    @Override
    public void merchantAccessQuery(ChannelMerchantAccessQueryContext channelMerchantAccessQueryContext) {

        log.info("银盈通进件查询请求参数:{}", JsonUtils.toJsonString(channelMerchantAccessQueryContext));
        CommonResult<YytPayConfig> yytPayConfigCommonResult = yytPayCommonParamsHandle.yytPayConfig(channelMerchantAccessQueryContext.getProviderChannelConfig(),
                channelMerchantAccessQueryContext.isTest());
        if (!yytPayConfigCommonResult.isSuccess()) {
            channelMerchantAccessQueryContext.setRemark(yytPayConfigCommonResult.getMsg());
            channelMerchantAccessQueryContext.setProcessStatus(false);
            return;
        }
        // 进件信息
        MerchantAccessInfoDO merchantAccessInfo = channelMerchantAccessQueryContext.getMerchantAccessInfo();
        // 银盈通配置信息
        YytPayConfig yytPayConfig = yytPayConfigCommonResult.getData();
        try{
            Map<String,Object> data = new HashMap<>();
            data.put("ent_no",yytPayConfig.getIspEntNo());
            data.put("query_type","01");

            String entName = merchantAccessInfo.getName().replace("(", "（").replace(")", "）");
            data.put("ent_name",entName);

            log.info("银盈通查询进件 请求参数 request:{}", JsonUtils.toJsonString(data));
            ReturnObj response = yytPayCommonParamsHandle.requestPostApi(YytPayConstant.URL_CA,"gas_partn_api@isp_ent_query",JSONUtil.toJsonStr(data),yytPayConfig);
            log.info("银盈通查询进件 返回参数 request:{}", JsonUtils.toJsonString(response));
            if(response.is_ok()){
                // 参数转化
                YytAccessQueryResp accessQueryResp = JSONUtil.toBean( JSONUtil.toJsonStr(response),YytAccessQueryResp.class);
                if("000000".equals(accessQueryResp.getOp_ret_subcode())){
                    if(CollUtil.isNotEmpty(accessQueryResp.getData())){
                        YytAccessQueryResp.EnInfo enInfo = accessQueryResp.getData().get(0);
                        /**
                         0 正常
                         1 实名中
                         3 实名失败
                         4 关停
                         5 注销
                         */
                        switch (enInfo.getEnt_state()) {
                            case "3":
                            case "4":
                            case "5":
                                channelMerchantAccessQueryContext.setProcessStatus(false);
                                channelMerchantAccessQueryContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
                                channelMerchantAccessQueryContext.setRemark(accessQueryResp.getOp_err_submsg());
                                break;
                            case "1":
                                break;
                            case "0":
                                channelMerchantAccessQueryContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.WAIT_SIGN);
                                break;
                            default:
                                channelMerchantAccessQueryContext.setProcessStatus(false);
                                channelMerchantAccessQueryContext.setRemark(response.get_err_msg());
                                break;
                        }
                    }
                }else{
                    channelMerchantAccessQueryContext.setProcessStatus(false);
                    channelMerchantAccessQueryContext.setRemark(accessQueryResp.getOp_err_submsg());
                }
            }else{
                channelMerchantAccessQueryContext.setProcessStatus(false);
                channelMerchantAccessQueryContext.setRemark(response.get_err_msg());
            }

        }catch (Exception e){
            log.error("银盈通查询进件请求失败,失败信息 message: {}", e);
            channelMerchantAccessQueryContext.setProcessStatus(false);
            channelMerchantAccessQueryContext.setRemark(e.getMessage());
        }
    }

    @Override
    public void merchantSign(ChannelMerchantAccessSignContext channelMerchantAccessSignContext) {

        CommonResult<YytPayConfig> yytPayConfigCommonResult = yytPayCommonParamsHandle.yytPayConfig(channelMerchantAccessSignContext.getProviderChannelConfig(),
                channelMerchantAccessSignContext.isTest());
        if (!yytPayConfigCommonResult.isSuccess()) {
            channelMerchantAccessSignContext.setRemark(yytPayConfigCommonResult.getMsg());
            channelMerchantAccessSignContext.setProcessStatus(false);
            return;
        }
        // 进件信息
        MerchantAccessInfoDO merchantAccessInfo = channelMerchantAccessSignContext.getMerchantAccessInfoDO();
        // 银盈通配置信息
        YytPayConfig yytPayConfig = yytPayConfigCommonResult.getData();
        try {

            Map<String,Object> data = new HashMap<>();
            data.put("ent_no",merchantAccessInfo.getSubMchId());
            data.put("isp_ent_no",yytPayConfig.getIspEntNo());
            data.put("login_token", "");//登录令牌
            data.put("req_no", IdUtil.simpleUUID());//请求流水号
            data.put("plat_form", "18");
            data.put("eSign_notify_type", "1");

            // 开通支付宝微信产品
            data.put("oper_type","01");
            data.put("aid","01");
            data.put("notify_url",(domain + MerchantExtConstant.MERCHANT_BUSINESS_SUCCESS_CALLBACK_URL + merchantAccessInfo.getChannelCode()
                    + StringPool.SLASH + merchantAccessInfo.getProviderCode()));

            // 获取费率信息
            Map<String,String> settle_circle = yytPayCommonParamsHandle.getsettleType(channelMerchantAccessSignContext.getMerchantFeeConfigList());
            log.info("银盈通 微信/支付宝业务签约 获取费率信息 :{}", JsonUtils.toJsonString(settle_circle));

            data.put("settle_circle", MapUtil.getStr(settle_circle,"settle"));

            List<Map<String,Object>> product_signs = new ArrayList<>();
            // 微信产品
            Map<String,Object> wx_product = new HashMap<>();
            wx_product.put("product_id","gpay_acq_prd_wx_payment");
            wx_product.put("value_add_service",yytPayCommonParamsHandle.getAddService(yytPayConfig));

            // 微信只有一个子产品
            Map<String,Object> wx_sub_product = new HashMap<>();
            wx_sub_product.put("charging_type","01");
            wx_sub_product.put("fee_type","01");
            wx_sub_product.put("fee_content", MapUtil.getStr(settle_circle,"wechat"));
            wx_product.put("sub_data", CollUtil.newArrayList(wx_sub_product));
            product_signs.add(wx_product);

            // 支付宝产品
            Map<String,Object> ali_product = new HashMap<>();
            ali_product.put("product_id","gpay_acq_prd_alipay_payment");
            ali_product.put("value_add_service",yytPayCommonParamsHandle.getAddService(yytPayConfig));
            // 支付宝只有一个子产品
            Map<String,Object> ali_sub_product = new HashMap<>();
            ali_sub_product.put("charging_type","01");
            ali_sub_product.put("fee_type","01");
            ali_sub_product.put("fee_content", MapUtil.getStr(settle_circle,"alipay"));
            ali_product.put("sub_data",CollUtil.newArrayList(ali_sub_product));
            product_signs.add(ali_product);

            // 产品明细
            data.put("product_signs",product_signs);

            log.info("银盈通 微信/支付宝业务签约 请求参数 :{}", JsonUtils.toJsonString(data));
            ReturnObj response = yytPayCommonParamsHandle.requestPostApi(YytPayConstant.URL_CA,"gas_partn_api@sign_bus_product",JSONUtil.toJsonStr(data),yytPayConfig);
            log.info("银盈通 微信/支付宝业务签约 返回参数 :{}", JsonUtils.toJsonString(response));

            if(response.is_ok()){
                // 参数转化
                YytAccessSignResp accessQueryResp = JSONUtil.toBean( JSONUtil.toJsonStr(response),YytAccessSignResp.class);
                if("000000".equals(accessQueryResp.getOp_ret_subcode())){
                    merchantAccessInfo.setStatus(MerchantEnum.ACCESS_STATUS.WAIT_SIGNING.getCode());
                    channelMerchantAccessSignContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.WAIT_SIGNING);
                    channelMerchantAccessSignContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.SIGN);
                }else{
                    // 产品开通审核中
                    if(!"600000".equals(accessQueryResp.getOp_ret_subcode())){
                        channelMerchantAccessSignContext.setProcessStatus(Boolean.FALSE);
                        channelMerchantAccessSignContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
                        channelMerchantAccessSignContext.setRemark(accessQueryResp.getOp_err_submsg());
                    }
                }
            }else{
                channelMerchantAccessSignContext.setProcessStatus(Boolean.FALSE);
                channelMerchantAccessSignContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
                channelMerchantAccessSignContext.setRemark(response.get_err_msg());
                log.error("银盈通 微信/支付宝业务签失败 message: {}", response.get_err_msg());
            }
        }
        catch (Exception e) {
            log.error("银盈通微信/支付宝业务配置请求失败,失败信息 message: {}", e);
            channelMerchantAccessSignContext.setRemark("银盈通微信/支付宝业务配置请求失败,失败信息" + e.getMessage());
            channelMerchantAccessSignContext.setProcessStatus(Boolean.FALSE);
        }

    }

    @Override
    public void merchantSignQuery(ChannelMerchantAccessSignContext channelMerchantAccessSignContext) {

        CommonResult<YytPayConfig> yytPayConfigCommonResult = yytPayCommonParamsHandle.yytPayConfig(channelMerchantAccessSignContext.getProviderChannelConfig(),
                channelMerchantAccessSignContext.isTest());
        if (!yytPayConfigCommonResult.isSuccess()) {
            channelMerchantAccessSignContext.setRemark(yytPayConfigCommonResult.getMsg());
            channelMerchantAccessSignContext.setProcessStatus(false);
            return;
        }
        // 进件信息
        MerchantAccessInfoDO merchantAccessInfo = channelMerchantAccessSignContext.getMerchantAccessInfoDO();
        // 银盈通配置信息
        YytPayConfig yytPayConfig = yytPayConfigCommonResult.getData();
        try {

            Map<String,Object> data = new HashMap<>();
            data.put("ent_no",merchantAccessInfo.getSubMchId());
            data.put("isp_ent_no",yytPayConfig.getIspEntNo());
            data.put("signature_flag","1");
            data.put("login_token", "");//登录令牌
            data.put("req_no", IdUtil.simpleUUID());//请求流水号
            data.put("plat_form", "18");

            log.info("银盈通 微信/支付宝业务签约查询 请求参数 :{}", JsonUtils.toJsonString(data));
            ReturnObj response = yytPayCommonParamsHandle.requestPostApi(YytPayConstant.URL_CA,"gas_partn_api@get_plat_form_enterprise",JSONUtil.toJsonStr(data),yytPayConfig);
            log.info("银盈通 微信/支付宝业务签约查询 返回参数 :{}", JsonUtils.toJsonString(response));

            if(response.is_ok()){
                // 参数转化
                YytAccessSignQueryResp accessQueryResp = JSONUtil.toBean(JSONUtil.toJsonStr(response),YytAccessSignQueryResp.class);
                if("000000".equals(accessQueryResp.getOp_ret_subcode())){
                    // 产品开通信息
                    if(StrUtil.isNotEmpty(accessQueryResp.getData())){
                        List<YytAccessProductResp> productResp = JSONUtil.toList(accessQueryResp.getData(),YytAccessProductResp.class);
                        log.info("银盈通 微信/支付宝业务签约查询 产品开通信息返回参数 :{}", JsonUtils.toJsonString(productResp));
                        channelMerchantAccessSignContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_SUCCESS);
                        channelMerchantAccessSignContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.FINISH);
                    }
                    // 签约链接
                    if(StrUtil.isNotEmpty(accessQueryResp.getSign_url())){
                        MerchantAccessSignLink merchantAccessSignLink = new MerchantAccessSignLink();
                        merchantAccessSignLink.setLink(accessQueryResp.getSign_url());
                        merchantAccessSignLink.setName("E签宝签章地址");
                        channelMerchantAccessSignContext.setSignLinks(CollUtil.newArrayList(merchantAccessSignLink));
                    }
                }else{
                    channelMerchantAccessSignContext.setProcessStatus(Boolean.FALSE);
                    channelMerchantAccessSignContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
                    channelMerchantAccessSignContext.setRemark(accessQueryResp.getOp_err_submsg());
                }
            }else{
                channelMerchantAccessSignContext.setProcessStatus(Boolean.FALSE);
                channelMerchantAccessSignContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
                channelMerchantAccessSignContext.setRemark(response.get_err_msg());
                log.error("银盈通 微信/支付宝业务签失败 message: {}", response.get_err_msg());
            }
        }
        catch (Exception e) {
            log.error("银盈通微信/支付宝业务配置请求失败,失败信息 message: {}", e);
            channelMerchantAccessSignContext.setRemark("银盈通微信/支付宝业务配置请求失败,失败信息" + e.getMessage());
            channelMerchantAccessSignContext.setProcessStatus(Boolean.FALSE);
        }

    }

    @Override
    public void merchantBusinessOpen(ChannelMerchantBusinessContext channelMerchantBusinessContext) {

    }

    @Override
    public void wechatConfigInit(ChannelMerchantBusinessContext channelMerchantBusinessContext) {

        channelMerchantBusinessContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.FINISH);
    }


    @Override
    public void wechatConfigAdd(ChannelMerchantBusinessContext channelMerchantBusinessContext) {

    }

    @Override
    public void wechatConfigQuery(ChannelMerchantAccessAuthQueryContext channelMerchantAccessAuthQueryContext) {

    }

    @Override
    public void wechatAuthConfig(ChannelMerchantBusinessContext channelMerchantBusinessContext) {


    }

    @Override
    public boolean authConfigQueryType() {
        return false;
    }

    @Override
    public void wechatAuthQuery(ChannelMerchantAccessAuthQueryContext channelMerchantAccessAuthQueryContext) {


    }

    @Override
    public void alipayAuthConfig(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
    }

    @Override
    public void alipayAuthQuery(ChannelMerchantAccessAuthQueryContext channelMerchantAccessAuthQueryContext) {


    }

    @Override
    public MerchantAccessCallBackDto accessCallBackParamsParse(HttpServletRequest request, ProviderChannelConfigDO providerChannelConfigDO) {

        MerchantAccessCallBackDto merchantAccessCallBackDto = new MerchantAccessCallBackDto();
        return merchantAccessCallBackDto;
    }

    @Override
    public CommonResult<String> accessCallbackParse(String callBackBody, List<ProviderChannelConfigDO> channelConfigDOList) {
        return null;
    }

    @Override
    public MerchantBusinessCallBackDto businessCallBackParamsParse(String callBackBody, ProviderChannelConfigDO providerChannelConfigDO) {
        MerchantBusinessCallBackDto merchantBusinessCallBackDto = new MerchantBusinessCallBackDto();
        try{
            // 配置信息
            YytPayConfig yytPayConfig = JsonUtils.parseObject(providerChannelConfigDO.getParamsConfig(), YytPayConfig.class);
            // 获取响应内容
            String result = yytPayCommonParamsHandle.decrypt(callBackBody,yytPayConfig,false);
            log.info("银盈通业务通知明文 plainText:{}",result);

        }catch (Exception e){
            log.error("银盈通业务配置回调请求失败,失败信息 message: {}", e);
        }

        return merchantBusinessCallBackDto;
    }

    @Override
    public boolean verify(String callBackBody, ProviderChannelConfigDO providerChannelConfigDO) {
        return false;
    }

    @Override
    public String callBackResponse(CommonResult commonResult) {
        if (commonResult.isSuccess()) {
            return "00";
        }
        else {
            return "FAIL";
        }
    }

    @Override
    public void termAdd(ChannelMerchantTermContext channelMerchantTermContext) {
    }

    @Override
    public void reloadMerchantAuth(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
    }

    @Override
    public void channelSplit(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
    }

    @Override
    public void productUpdate(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
    }

    @Override
    public Integer queryMerAuthStatus(ChannelMerchantAccessAuthQueryContext channelMerchantAccessAuthQueryContext) {
        return null;
    }

    @Override
    public MerchantProductQueryResp productQuery(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
        return null;
    }

    @Override
    public MerchantChannelSplitCallBackDto easySplitCallBackParamsParse(HttpServletRequest request, ProviderChannelConfigDO providerChannelConfigDO) {
        return null;
    }


}
